package com.briup.corejava.chap12;

import java.io.IOException;
import java.net.*;

/**
 * UDP协议下数据发送端
 */
public class Test072_UdpSender {
    public static void main(String[] args) {
        String ip = "127.0.0.1";
        int port = 8888;
        DatagramSocket socket = null;
        DatagramPacket packet = null;

        try {
            socket = new DatagramSocket();
            // 数据使用byte[]来进行发送
            byte[] data = "今天是个星期五。".getBytes();
            packet = new DatagramPacket(data,0,data.length, InetAddress.getByName(ip),port);
            // 发送数据
            socket.send(packet);
            System.out.println("数据包发送完成...");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
